﻿using System;
using System.Text;

namespace NewUI.Service.Common;

public class PlcJuanCaiInfo 
{
    // 字段定义
    public int Id;
    public string BianHao = "";
    public short HouDu;
    public short ChangDu;
    public short KuanDu;
    public short ZhiJiang;
    public string Treatment = "";
    public string ZhongLiang = "0Kg";
    public string DengJi = "";
    public string JianYanYuan = "";
    public string RiQi = "1990-1-1";
    public string Code = "";
    public string PiCi = "";
    public string Print_Template = "测试";
    public string KeHu_Name = "";
    public short DJ_class;
    public bool No_Pack;
    public bool KH_OK;
    public bool Print_Top_OK;
    public bool Print_Body_OK;
    public bool Need_QPM;
    public bool Need_ZG;
    public bool Need_PMD;
    public bool IS_HouPMD;
    public bool Double_PMD;
    public bool Need_TieBiao_CM;
    public bool Need_TieBiao_Middle;
    public bool Multi_MiddleTieBiao;
    public bool LM_OK;
    public bool RR_OK;
    public bool DZ_Tetect_OK;
    public bool HunBai;
    public float KuanDu_Add;
    public short From_SSD_No;
    public short TuoPan_No;
    
    private const int StructSize = 636;

    public void ParseSource(byte[] content)
    {
        // 基本字段解析
        Id = BitConverter.ToInt32([content[3], content[2], content[1], content[0]], 0);
        BianHao = ParseS7String(content, startOffset: 4, maxLength: 30, encoding: Encoding.ASCII);
        Treatment = ParseS7String(content, startOffset: 44, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        ZhongLiang = ParseS7String(content, startOffset: 108, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        DengJi = ParseS7String(content, startOffset: 174, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        JianYanYuan = ParseS7String(content, startOffset: 240, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        RiQi = ParseS7String(content, startOffset: 304, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        Code = ParseS7String(content, startOffset: 368, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        PiCi = ParseS7String(content, startOffset: 432, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        Print_Template = ParseS7String(content, startOffset: 498, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        KeHu_Name = ParseS7String(content, startOffset: 562, maxLength: 30, encoding: Encoding.BigEndianUnicode);
        
        // 解析数值字段
        HouDu = BitConverter.ToInt16([content[37], content[36]], 0);
        ChangDu = BitConverter.ToInt16([content[39], content[38]], 0);
        KuanDu = BitConverter.ToInt16([content[41], content[40]], 0);
        ZhiJiang = BitConverter.ToInt16([content[43], content[42]], 0);
        DJ_class = BitConverter.ToInt16([content[237], content[236]], 0);
        From_SSD_No = BitConverter.ToInt16([content[627], content[626]], 0);
        TuoPan_No = BitConverter.ToInt16([content[629], content[628]], 0);
        
        // 解析布尔字段
        No_Pack = content[494] != 0;
        HunBai = content[630] != 0;
        
        // 解析字节 624 (地址 624.0)
        byte byte624 = content[624];
        KH_OK = (byte624 & (1 << 0)) != 0;
        Print_Top_OK = (byte624 & (1 << 1)) != 0;
        Print_Body_OK = (byte624 & (1 << 2)) != 0;
        Need_QPM = (byte624 & (1 << 3)) != 0;
        Need_ZG = (byte624 & (1 << 4)) != 0;
        Need_PMD = (byte624 & (1 << 5)) != 0;
        IS_HouPMD = (byte624 & (1 << 6)) != 0;
        Double_PMD = (byte624 & (1 << 7)) != 0;

        // 解析字节 625 (地址 625.0)
        byte byte625 = content[625];
        Need_TieBiao_CM = (byte625 & (1 << 0)) != 0;
        Need_TieBiao_Middle = (byte625 & (1 << 1)) != 0;
        Multi_MiddleTieBiao = (byte625 & (1 << 2)) != 0;
        LM_OK = (byte625 & (1 << 3)) != 0;
        RR_OK = (byte625 & (1 << 4)) != 0;
        DZ_Tetect_OK = (byte625 & (1 << 5)) != 0;
        
        // 解析浮点数 (大端序)
        KuanDu_Add = BitConverter.ToSingle(new byte[] { 
            content[635], content[634], content[633], content[632] 
        }, 0);
    }

    private string ParseS7String(
        byte[] content,     // 整个数据块的字节数组
        int startOffset,    // 字符串结构在字节数组中的起始位置
        int maxLength,      // 字符串的最大允许长度
        Encoding encoding   // 字符串的编码方式
    )
    {
        try
        {
            // 获取实际字符串长度
            int actualLength = content[startOffset + 1];
        
            // 安全校验
            if (actualLength <= 0 || actualLength > maxLength) 
                return string.Empty;
        
            // 计算数据起始位置和字节长度
            int dataStart = startOffset + 2;
            int byteCount = encoding == Encoding.ASCII 
                ? actualLength 
                : actualLength * 2;  // Unicode 字符占2字节
        
            // 提取字符串内容并解码
            string result = encoding.GetString(content, dataStart, byteCount);
        
            // 对于 WString 类型，处理特殊字符和尾随空字符
            if (encoding == Encoding.BigEndianUnicode)
            {
                // 移除尾随的空字符
                result = result.TrimEnd('\0');
            
                // 移除开头的控制字符 (如 \u000f) - 兼容 .NET 9.0 的方法
                result = RemoveLeadingControlCharacters(result);
            }
        
            return result;
        }
        catch
        {
            return string.Empty;
        }
    }

    // 兼容 .NET 9.0 的方法：移除开头的控制字符
    private string RemoveLeadingControlCharacters(string input)
    {
        if (string.IsNullOrEmpty(input)) 
            return input;
    
        int startIndex = 0;
    
        // 找到第一个非控制字符的位置
        while (startIndex < input.Length && char.IsControl(input[startIndex]))
        {
            startIndex++;
        }
    
        return input.Substring(startIndex);
    }

}